﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;

namespace SLAssetSystem
{

    public class AssetSystem : Singleton<AssetSystem>, IInitable
    {
        public ModeType ModeType
        {
            get
            {
                return modeType;
            }
        }
        private ModeType modeType = ModeType.Debug;

        private Dictionary<AssetType, IResAdapter> resAdapters = new Dictionary<AssetType, IResAdapter>();


        public void Init()
        {
            if (!IsInited)
            {
                resAdapters.Add(AssetType.CSV, new CsvResAdapter());
                resAdapters.Add(AssetType.Json, new JsonResAdapter());

            }
        }

        public T LoadResAsset<T>(string path, string name, AssetType assetType = AssetType.GameObject) where T : UnityEngine.Object
        {
            return LoadResAsset<T>(path + name, assetType);
        }

        public T LoadResAsset<T>(string path, AssetType assetType = AssetType.GameObject) where T : UnityEngine.Object
        {
            UnityEngine.Object asset;
            if (modeType == ModeType.Debug)
            {
                asset = Resources.Load(path);
            }
            else
            {
                asset = null;
            }

            switch (assetType)
            {
                case AssetType.GameObject:
                    return (T)asset;
                case AssetType.ScriptableObject:
                case AssetType.AssetBundle:
                case AssetType.CSV:
                    return resAdapters[AssetType.CSV].TransformRes<T>(asset);
                case AssetType.Json:
                    return resAdapters[AssetType.Json].TransformRes<T>(asset);
                default:
                    return null;
            }
        }

        public T LoadAsset<T>(FileInfo info, AssetType assetType = AssetType.GameObject) where T : UnityEngine.Object
        {
            return null;
        }

        public void LoadAssetAsync<T>(string path, string name, Action<T> onLoadCompleted)
        {

        }
    }
}
